package Util::UserStore::UserStore;
use Moose;
use namespace::autoclean;
use JSON;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub get_top_ten{
	my ($self) = @_;

	my @stores = $self->schema->resultset('Store')->search(
		{ 
			'user_stores.role' => 'T',
			'user_stores.notify_status' => 'N',	
		},
		{
			'+select' => [
				{sum => ["CASE WHEN user_stores.role = 'T' THEN CASE WHEN user_stores.notify_status = 'N' THEN 1 ELSE 0 END ELSE 0 END"], -as=> 'user_count'}
			],
			row => 5,
			page => 1,
			order_by => {-desc => [qw/user_count/]},			
			#group_by => 'user_stores.user_id',
			group_by => 'user_stores.stores_id',
			join => 'user_stores'
		}	
	);
	return \@stores;
}
sub userstore_search {
	my ($self, $user_id, $store_id, $role) = @_;

	unless(defined($role)) {
		return $self->schema->resultset('UserStore')->search({user_id => $user_id, stores_id => $store_id }, {});
	} else {
		return $self->schema->resultset('UserStore')->search(
			{user_id => $user_id, 
			role => $role, 
			stores_id => $store_id }, 
		{});
	}
}
sub get_all_members {
	my ($self, $store_id) = @_;

	return $self->schema->resultset('UserStore')->search({stores_id => $store_id }, {});
}

sub userstore_search_by_status {
	my ($self, $user_id, $store_id,$status, $role) = @_;

	unless(defined($role)) {
		return $self->schema->resultset('UserStore')->search({user_id => $user_id, stores_id => $store_id }, {});
	} else {
		return $self->schema->resultset('UserStore')->search(
			{user_id => $user_id, 
			role => $role, 
			stores_id => $store_id,
			notify_status => $status 
			}, 
		{});
	}
}

sub get_store_owners {
	my ($self, $stores, $c) = @_;

	my %store_owners;
        foreach (@$stores) {
                my $store_id = $_->get_column("id");
                my $user_store = $self->schema->resultset('UserStore')->search({stores_id => $store_id, role => 'C'})->first();
                my $owner = $self->schema->resultset('User')->find($user_store->user_id);

                $store_owners{$store_id}{user_id} = $owner->get_column("id");
                $store_owners{$store_id}{user_name} = $owner->get_column("username");
        }

	return \%store_owners;
}

sub get_owner {
	my ($self, $store_id) = @_;
	return $self->schema->resultset('UserStore')->search({stores_id => $store_id, role => 'C',});
}

sub get_store_user {
	my ($self, $store_id) = @_;
	return $self->schema->resultset('UserStore')->search({stores_id => $store_id, role => 'T',});
}

sub get_user_stores {
	my ($self, $user_id) = @_;
	return $self->schema->resultset('UserStore')->search({user_id => $user_id, role => 'T',});
}

sub get_owner_user {
	my ($self, $store_id) = @_;
	my $user_store = $self->schema->resultset('UserStore')->search({stores_id => $store_id, role => 'C',})->first();
        return $self->schema->resultset('User')->find($user_store->user_id);
}

sub get_unnotified_user {
	my ($self, $store_id) = @_;
	return $self->schema->resultset('UserStore')->search({stores_id => $store_id, role => 'T', notify_status => 'N'});
}

sub update_notify_status {
	my ($self, $store_id) = @_;
	my @user_stores = $self->schema->resultset('UserStore')->search({stores_id => $store_id, role => 'T',});
	foreach my $user_store(@user_stores)	
	{
		$user_store->update({
			notify_status => 'Y'
		});
	}
        return @user_stores;
}

sub update_notify_status_to_no {
	my ($self, $store_id, $user_id) = @_;
	my $user_store = $self->schema->resultset('UserStore')->search({stores_id => $store_id, user_id => $user_id, role => 'T',})->first();
	$user_store->update({
		notify_status => 'N'
	});
        return $user_store;
}

__PACKAGE__->meta->make_immutable;

1;
